package com.crack.nga2.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/22.
 */
public class TagUtil {
    public static String getString(String org) {
        String string;
        StringBuilder sb = new StringBuilder(org);
        int index_left = -1;
        int index_right = -1;
        while ((index_left = org.indexOf("[", index_left + 1)) != -1
                && (index_right = org.indexOf("]", index_left + 1)) != -1) {
            string = org.substring(index_left, index_right + 1);
            if (string.startsWith("[b") ||
                    string.startsWith("[/b") ||
                    string.startsWith("[color") ||
                    string.startsWith("[/color") ||
                    string.startsWith("[size") ||
                    string.startsWith("[/size") ||
                    string.startsWith("[img") ||
                    string.startsWith("[/img") ||
                    string.startsWith("[url") ||
                    string.startsWith("[/url") ||
                    string.startsWith("[del") ||
                    string.startsWith("[/del") ||
                    string.startsWith("[font") ||
                    string.startsWith("[/font")
                    ) {
                sb.replace(index_left, index_right + 1, "<" + string.substring(1, string.length() - 1) + ">");
            }
        }
        return parseReply(sb.toString());
    }

    /**
     * 替换[pid]Reply [/pid]
     * 删除Post by [uid]
     */
    public static String parseReply(String str) {
        str = str.replaceAll("(Reply to )?\\[pid.*\\]Reply\\[/pid\\]", "回复:");
        str = str.replaceAll("Post by \\[uid=\\d*\\]", "<uid>");
        str = str.replaceAll("\\[/uid\\]", "</uid>");
        return str;
    }

    public static String getQuote(String content) {
        return getTagContent(content, "quote");
    }

    /**
     * 截取标签中的内容
     */
    public static String getTagContent(String content, String tag) {
        int s;
        int e;
        if ((s = content.indexOf("[" + tag + "]")) != -1 && (e = content.lastIndexOf("[/" + tag + "]")) != -1) {
            return content.substring(s + tag.length() + 2, e);
        }
        return null;
    }

    /**
     * 截取引用内容
     */
    public static List<String> getQuotes(String string) {
        List<String> list = new ArrayList<>();
        int index = -1;
        int end = -1;
        int length = "[quote]".length();
        while ((index = string.indexOf("[quote]", index + 1)) != -1) {
            if (index < end) {
                //异常数据
                return null;
            }
            end = string.indexOf("[/quote]", end + 1);
            String s = string.substring(index + length, end );
            list.add(s);
        }
        return list;
    }

    /**
     * 删除引用内容
     */
    public static String deleteQuote(String string) {
        StringBuilder sb = new StringBuilder(string);
        int s = sb.indexOf("[quote]");
        int e = sb.lastIndexOf("[/quote]");
        if (s != -1 && e != -1) {
            sb.delete(s, e + "[/quote]".length());
        }
        return sb.toString();
    }
}
